package org.samoji.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Book implements CRUDInterface {
	private String title;
	private String publishYear;
	private String publisher;

	public Book(String title, String publishYear, String publisher) {
		this.title = title;
		this.publishYear = publishYear;
		this.publisher = publisher;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @return the publishYear
	 */
	public String getPublishYear() {
		return publishYear;
	}

	/**
	 * @return the publisher
	 */
	public String getPublisher() {
		return publisher;
	}

	@Override
	public String toString() {
		StringBuffer s = new StringBuffer("Title: " + title);
		s.append("\n Publisher: " + publisher);
		s.append("\n Publish year: " + publishYear);

		return s.toString();
	}

	@Override
	public void Create(Object o) {
		
	}

	@Override
	public void Read(Object o) {
		
	}

	@Override
	public void Update(Object o) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Delete(Object o) {
		// TODO Auto-generated method stub
		
	}
	
	public static ArrayList<Book> getAll() throws SQLException {
		String sql = "SELECT * FROM LIBRI";
		PreparedStatement pst;

		pst = Database.getInstance().getConnection().prepareStatement(sql);

		ResultSet rs = pst.executeQuery();
		ArrayList<Book> allBooks = new ArrayList<Book>();
		
		while (rs.next()) {
			allBooks.add(
					new Book(
							rs.getString(1),
							rs.getString(2),
							rs.getString(3)
					)
			);
		}
		return allBooks;
	}	
}